﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using PMS.XCMG.Util;

namespace PMS.XCMG.Entity
{
    /// <summary>
    /// 计划状态
    /// </summary>
    public enum PlanStatus
    {
        /// <summary>
        /// 编制
        /// </summary>
        Staffing = 0,
        /// <summary>
        /// 已下发
        /// </summary>
        Issued = 1,
        /// <summary>
        /// 执行中
        /// </summary>
        Executing = 2,
        /// <summary>
        /// 生产完成
        /// </summary>
        Completed = 3,
        /// <summary>
        /// 强制办结
        /// </summary>
        ManualEnd = 4
    }

    /// <summary>
    /// 物料配送状态
    /// </summary>
    public enum DeliveryStatus
    {
        /// <summary>
        /// 未开始
        /// </summary>
        NotStarted=0,
        /// <summary>
        /// 执行中
        /// </summary>
        Executing = 1,
        /// <summary>
        /// 配送完成
        /// </summary>
        Completed = 2
    }

    /// <summary>
    /// 计划
    /// </summary>
    public partial class Plan : BusEntity
    {
        public Plan()
        {
            CreateTime = DateTime.Now;
        }

        /// <summary>
        /// 计划编码
        /// </summary>
        public string Code { get; set; }
        /// <summary>
        /// 计划名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 产品BomId
        /// </summary>
        public string BomId { get; set; }
        /// <summary>
        /// 计划数量（按台套）
        /// </summary>
        public int Count { set; get; }
        /// <summary>
        /// 实际完成数量（按台套）
        /// </summary>
        public int Quantity { set; get; }
        /// <summary>
        /// 计划日期
        /// </summary>
        public string PlanDate { set; get; }
        /// <summary>
        /// 计划日期
        /// </summary>
        public int PlanDay { set; get; }
        /// <summary>
        /// 计划班时
        /// </summary>
        public string TeamTimeCode { set; get; }
        /// <summary>
        /// 计划状态
        /// </summary>
        public PlanStatus Status { set; get; }
        /// <summary>
        /// 物料配送状态
        /// </summary>
        public DeliveryStatus DeliveryState { set; get; }
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime { set; get; }
        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { set; get; }
    }

    public partial class Plan : BusEntity
    {
        /// <summary>
        /// Bom
        /// </summary>
        public MaterialBom Bom { set; get; }

        /// <summary>
        /// 计划备料实体清单
        /// </summary>
        public List<PlanPrepare> Prepares { set; get; }

        /// <summary>
        /// 计划物料预收单
        /// </summary>
        public List<Precollected> Precollecteds { set; get; }
    }

    /// <summary>
    /// 实体配置
    /// OnModelCreating
    /// </summary>
    public class PlanTypeConfig : BusEntityTypeConfig<Plan>, IEntityTypeConfiguration<Plan>
    {
        public override void Configure(EntityTypeBuilder<Plan> builder)
        {
            base.Configure(builder);

            #region 主外键关系
            builder.HasOne(p => p.Bom).WithMany().HasForeignKey(p => p.BomId);
            #endregion

            #region 字段属性:最大长度,是否必需,默认值
            builder.Property(p => p.Code).HasMaxLength(EntityDefinition.DefSmallColLen).IsRequired();
            builder.Property(p => p.Name).HasMaxLength(EntityDefinition.DefSmallColLen).IsRequired();
            builder.Property(p => p.BomId).HasMaxLength(EntityDefinition.DefSmallColLen).IsRequired();
            builder.Property(p => p.Count).IsRequired();
            builder.Property(p => p.Quantity).IsRequired();
            builder.Property(p => p.PlanDate).HasMaxLength(EntityDefinition.DefSmallColLen).IsRequired();
            builder.Property(p => p.PlanDay).IsRequired();
            builder.Property(p => p.TeamTimeCode).HasMaxLength(EntityDefinition.DefSmallColLen);
            builder.Property(p => p.Status).IsRequired();
            builder.Property(p => p.DeliveryState).IsRequired();
            builder.Property(p => p.StartTime);
            builder.Property(p => p.EndTime);
            #endregion

            #region 备注
            builder.HasComment("计划");
            builder.Property(p => p.Code).HasComment("计划编码");
            builder.Property(p => p.Name).HasComment("计划名称");
            builder.Property(p => p.BomId).HasComment("产品BomId");
            builder.Property(p => p.Count).HasComment("计划数量（按台套）");
            builder.Property(p => p.Quantity).HasComment("实际完成数量（按台套）");
            builder.Property(p => p.PlanDate).HasComment("计划日期");
            builder.Property(p => p.PlanDay).HasComment("计划日期");
            builder.Property(p => p.TeamTimeCode).HasComment("计划班时");
            builder.Property(p => p.Status).HasComment("计划状态");
            builder.Property(p => p.DeliveryState).HasComment("计划备料状态");
            builder.Property(p => p.StartTime).HasComment("开始时间");
            builder.Property(p => p.EndTime).HasComment("结束时间");
            #endregion

            #region 种子数据
            #endregion
        }
    }
}
